/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.master;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import softstarter.filehandling.FileCentre;


public class DragAndDropHandler implements DropTargetListener {
    
    private DropTarget dropTarget;
    private GUIStateController controller;
    
    public DragAndDropHandler(JFrame dropTargetArea, GUIStateController controller) {
        this.controller = controller;
        dropTarget = new DropTarget(dropTargetArea, this);
    }
    
    @Override
    public void dragEnter(DropTargetDragEvent arg0) {
        
    }

    @Override
    public void dragExit(DropTargetEvent arg0) {
        
    }

    @Override
    public void dragOver(DropTargetDragEvent arg0) {
        
    }

    @Override
    public void drop(DropTargetDropEvent dropEvent) {
        final List result = new ArrayList();
        int action = dropEvent.getDropAction();
        dropEvent.acceptDrop(action);
        try {
            Transferable data = dropEvent.getTransferable();
            DataFlavor flavors[] = data.getTransferDataFlavors();
            if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                List<File> files = (List<File>) data.getTransferData(
                    DataFlavor.javaFileListFlavor);
                FileCentre.getInstance().addJavaFiles(files);
                controller.updateFileView();
                controller.refreshGUI();       
            }
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dropEvent.dropComplete(true);
        }       
    }

    @Override
    public void dropActionChanged(DropTargetDragEvent arg0) {
    
    }

}
